package com.wizz.trade.template.controller;


import com.wizz.trade.common.ResponseResult;
import com.wizz.trade.common.constant.StatusCode;
import com.wizz.trade.template.entity.TemplateCategory;
import com.wizz.trade.template.entity.Template;
import com.wizz.trade.template.entity.vo.ContractTemplateMetaInfoVo;
import com.wizz.trade.template.service.TemplateService;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 模板模块
 */
@RestController
@RequestMapping("/template")
public class TemplateController {

    @Resource
    private TemplateService templateService;

    @Resource
    private ServletContext servletContext;

    /**
     * 查看模版的分类
     *
     * @return 返回模版的分类信息
     */
    @GetMapping("/category")
    public ResponseResult<List<TemplateCategory>> getCategory() {
        ArrayList<TemplateCategory> res = new ArrayList<>();
        res.add(new TemplateCategory(1, "合同模版"));
        res.add(new TemplateCategory(2, "提单模版"));
        res.add(new TemplateCategory(3, "付款申请模版"));
        res.add(new TemplateCategory(4, "首款确认模版"));
        res.add(new TemplateCategory(5, "对账单模版"));
        return new ResponseResult<>(StatusCode.OK, res);
    }


    // TODO: 2023/8/5 查看信息


    /**
     * 根据模版分类id查看该分类下所有模版信息
     *
     * @param id 模版的分类ID
     * @return 具体信息
     */
    @GetMapping("/category/{id}")
    public ResponseResult<List<Template>> getTemplateMetaList(@PathVariable Integer id) {
        ArrayList<Template> res = templateService.getTemplateMetaInfo(id);
        return new ResponseResult<>(StatusCode.OK, res);
    }


    /**
     * 根据模版id获取模版详细配置信息
     *
     * @param id
     * @return
     */
    @GetMapping("/generate/{id}")
    public ResponseResult<Template> getTemplateDetails(@PathVariable Integer id) {
        Template res = templateService.getTemplateDetailsByTemplateId(id);
        return new ResponseResult<>(StatusCode.OK, res);
    }


    /**
     * 根据模版id和用户自定义数据导出excel表
     *
     * @param resp   返回导出的excel
     * @param id     模版id
     * @param params 传入的details实体配置类
     */
    @PostMapping(value = "/export/{id}", produces = "application/vnd.ms-excel")
    public void exportExcel(HttpServletResponse resp, @PathVariable Integer id, @RequestBody Map<String, Object> params) {
        templateService.export(resp, id, params);
        resp.setContentType("application/vnd.ms-excel");
    }

    /**
     * 根据模版id 预览相关图片
     *
     * @param response 返回的内容
     * @param id       模版ID
     * @throws IOException
     */
    @GetMapping("/preview/{id}")
    public void preview(HttpServletResponse response, @PathVariable Integer id) throws IOException {
        InputStream in = servletContext.getResourceAsStream("/template/service_document/采购-合同模板预览.png");
        response.setContentType(MediaType.IMAGE_PNG_VALUE);
        IOUtils.copy(in, response.getOutputStream());
    }


//
//
//    @GetMapping("/list/contract")
//    public ResponseResult<List<ContractTemplateMetaInfoVo>> listContract(){
//        templateService.listContract()
//    }
//
//    public ResponseResult createContract(ContractTemplateMetaInfoDto contractTemplateMetaInfo){
//
//    }

}
